//
//  ConversationingViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/20.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class ConversationingViewController: UIViewController,YZPullDownMenuDataSource {
    var tableView : UITableView!
    
    var titleString : String!
    
    var nameArray : [String] = ["产业分类","县市区","进度"]
    var ConversationingArrays = [ConversationingModel]()
    
    var taskNameString : String!
    
    var projectStage:String!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        print(taskNameString)

        self.view.backgroundColor = UIColor.white
        
       self.title = titleString

         let name =  YZPullDownMenu()
        name.frame = CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: 44)
        name.dataSource = self
        
        self.view.addSubview(name)
        
        
        self.setupAllChildViewController()
        
        tableView = UITableView(frame:CGRect(x: 0, y: 108, width: self.view.frame.size.width, height: self.view.frame.size.height - name.frame.size.height), style:.grouped)

        tableView.dataSource = self
        tableView.delegate = self


        tableView.tableFooterView = UIView.init()

       self.view.addSubview(tableView)


        tableView.register(NSClassFromString("MainConversationingCell"), forCellReuseIdentifier: "MainConversationingCell")
        
        self.loadData()
    }
    func loadData() {
        
        let start:String? = "1"
        let pageSize:String? = "5"
       
        let manager = UserDefaults()
        let token: String? = manager.string(forKey:"tokens")
        
         let quurlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/adminUserCenter/getMyTask"
        
      
        let json = "{\"token\":\"\(token!)\",\"start\":\"\(start!)\",\"pageSize\":\"\(pageSize!)\",\"taskName\":\"\(taskNameString!)\"}"

        
        print("json====\(json)")
        
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        let url = URL(string: quurlString)!
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("response====\(response)")
      
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    let model =   zaitanModel()
                    
                    
               
                    
                    
                    
                 
                    
                    
                }
                
                self.tableView.reloadData()
                
                
            }else{
                
                
            }
        }
        
        
        
        
        
    }
    func setupAllChildViewController()  {
        let chanyefenlei = chanyefenleiViewController()
        let xianshiqu = xianshiquViewController()
        let zaitan = zaitanViewController()
        self.addChildViewController(chanyefenlei)
        self.addChildViewController(xianshiqu)
        self.addChildViewController(zaitan)
    }
    func numberOfCols(in pullDownMenu: YZPullDownMenu!) -> Int {
        return 3
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, buttonForColAt index: Int) -> UIButton! {
        
        let button = YZMenuButton(type: .custom)
        

        
        
        button.setTitle(nameArray[index], for: .normal)
        
        button.setTitleColor(UIColor.black, for: .normal)
        
        button.setImage(UIImage(named: "标签-向下箭头"), for: .normal)
        
        button.setImage(UIImage(named: "标签-向上箭头"), for: .selected)
        
        return button
        
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, viewControllerForColAt index: Int) -> UIViewController! {
        return self.childViewControllers[index]
    }
    
    func pullDownMenu(_ pullDownMenu: YZPullDownMenu!, heightForColAt index: Int) -> CGFloat {
        return 180
    }
    
  
        
}
    


extension ConversationingViewController: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 10


    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 40

    }

    func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
        return 10
    }




    //返回分区头部视图
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {



            let headerView = UIView()
            headerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 40)
            headerView.backgroundColor = UIColor.white
            let titleLabel = UILabel()
            titleLabel.text = "项目名称"
            titleLabel.textAlignment = .left
            titleLabel.textColor = UIColor.black
            titleLabel.font = UIFont.systemFont(ofSize:14)//一般方法
            titleLabel.sizeToFit()

            headerView.addSubview(titleLabel)


            let headerButton = UILabel()
            headerButton.font = UIFont.systemFont(ofSize:14)//一般方法
            headerButton.text = "项目进度"
            headerButton.textAlignment = .center
            headerButton.textColor = UIColor.black

            headerButton.sizeToFit()


            headerView.addSubview(headerButton)



            titleLabel.snp.makeConstraints { (make) in
                make.top.equalTo(headerView.snp.top).offset(5)
                make.left.equalTo(headerView.snp.left).offset(10)
                make.width.equalTo(100)
                make.height.equalTo(30)
            }


            headerButton.snp.makeConstraints { (make) in
                make.top.equalTo(headerView.snp.top).offset(5)
                make.right.equalTo(headerView.snp.right).offset(-10)
                make.height.equalTo(30)
            }




            return headerView


    }
    //返回分区头部高度
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {


            return 40


    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {



            let cell = MainConversationingCell(style:UITableViewCellStyle.default, reuseIdentifier: "MainConversationingCell")
//
//            let cellModel:ConversationingModel = self.ConversationingArrays[indexPath.row]
//            cell.ConversationingWithModel(model: cellModel)
            return cell


    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



    }

}
